<?php

namespace App\Http\Service\Customers;

use App\Func\Customer\CustomerFunc;
use App\Func\File\FileFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\CompanyDepartment\DepartmentModel;
use App\Models\Customers\CustomersModel;

class ManageCustomersService implements BasicInterfaceService
{

    public function listSelectDefaultFilters($query){
        $query->pushFilter('status',1);
    }


    public function getList(array $prams)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $customerModel = new CustomersModel();
        $this->listSelectDefaultFilters($customerModel);
        if($filters){
            $this->listSelectFilters($customerModel,$filters);
        }
        $res = $customerModel->cList($customerModel,$page_size,$desc,[
            'com_id',
            'name',
            'avatar_id',
            'email',
            'phone',
            'union_id',
            'created_at as created_time',
            'updated_at as updated_time',
            'staff_last_login_time',
        ]);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.customer')
        ]));
    }

    public function dispose($data): mixed
    {
        return $data;
    }

    public function listSelectFilters($query, $filters)
    {
        if(map_has_value($filters,'com_id')){
            $query->pushFilter('com_id',$filters['com_id']);
        }
        if(map_has_value($filters,'name')){
            $query->pushFilter('name',$filters['name'],'like');
        }
        if(map_has_value($filters,'email')){
            $query->pushFilter('email',$filters['email'],'like');
        }
        if(map_has_value($filters,'phone')){
            $query->pushFilter('phone',$filters['phone'],'like');
        }
    }

    public function getSelect()
    {
        
    }

    public function getSelectHasPage($prams)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $customerModel = new CustomersModel();
        $this->listSelectDefaultFilters($customerModel);
        if($filters){
            $this->listSelectFilters($customerModel,$filters);
        }
        $res = $customerModel->cList($customerModel,$page_size,$desc,[
            'name as value'
        ]);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.customer')
        ]));
    }

    public function getInfo($id)
    {
        $customerModel = new CustomersModel();
        $res = $customerModel->with('address')
            ->where('customer_id',$id)
            ->where('status',1)
            ->first();
        $res->avatar_files = (new FileFunc())->getFileInfoByFileIdToWeb($res->avatar_id);
        s_c_return($res,trans('message.get_detail_ok',[
            'name' => trans('key.customer')
        ]));
    }

    public function create($data)
    {

    }

    public function fakeDelete($id)
    {

    }

    public function fakeDeleteByIds($ids)
    {
        
    }

    public function delete($id)
    {
        
    }

    public function update($data)
    {

    }
}
